//
//  QZHomeRecomeFollwView.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/8/27.
//

#import "QZHomeRecomeFollwView.h"
#import "SYCollectionViewRoundFlowLayout.h"
#import "QZHomeFollowUserCell.h"
#import "QZHomeFollowUserHeaderView.h"
#import "QZOtherMyVC.h"
#import "QZMyDetailsPlayVC.h"

@interface QZHomeRecomeFollwView ()
@property (assign, nonatomic)CGSize itemSize;
@property (strong, nonatomic) UICollectionView *followCollection;
@end
@implementation QZHomeRecomeFollwView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat w = (KScreen_W-3)/2;
        _itemSize = CGSizeMake(w, (w/180)*240);
        
        
        SYCollectionViewRoundFlowLayout *layout = [[SYCollectionViewRoundFlowLayout alloc]init];
        layout.sectionHeadersPinToVisibleBounds = YES;
        layout.collectionCellAlignmentType = SYCollectionViewFlowLayoutAlignmentTypeByLeft;
        
        self.followCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, KScreen_H-KTab_H) collectionViewLayout:layout];
        self.followCollection.backgroundColor = viewback_black;
        if (@available(iOS 11.0, *)) {
            self.followCollection.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self addSubview:self.followCollection];
        
        [_followCollection registerNib:[UINib nibWithNibName:@"QZHomeFollowUserCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QZHomeFollowUserCell"];
        [_followCollection registerNib:[UINib nibWithNibName:@"QZHomeFollowUserHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"QZHomeFollowUserHeaderView"];
        _followCollection.backgroundColor = viewback_black;
        _followCollection.delegate = self;
        _followCollection.dataSource = self;
    }
    return self;
}
-(void)configUser:(NSArray *)userList{
    self.userList = userList;
    [_followCollection reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.userList.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QZUserInfoModel * model = self.userList[indexPath.row];
    QZHomeFollowUserCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZHomeFollowUserCell" forIndexPath:indexPath];
    [cell configUserModel:model];
    KWeakSelf
    cell.QZUserFollowHomeBlock = ^(NSInteger tag) {
        if (tag == 0) {
            [weakSelf followsUser:indexPath.row];
        }else{
            [QZHongBaoView share].hidden = YES;
            [[QZHongBaoView share] stopClick:NO];
            QZUserInfoModel * model = weakSelf.userList[indexPath.row];
            QZOtherMyVC * other = [[QZOtherMyVC alloc]init];
            other.otherMid = model.mid;
            [[Tools currentActiveController].navigationController pushViewController:other animated:YES];
        }
        
    };
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return _itemSize;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(KScreen_W, 55+KNav_H);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        QZHomeFollowUserHeaderView * header = (QZHomeFollowUserHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"QZHomeFollowUserHeaderView" forIndexPath:indexPath];
        header.backgroundColor = viewback_black;
        return header;
    }else{
        UICollectionReusableView * header = nil;
        return header;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    QZUserInfoModel * model = _userList[indexPath.row];
//    QZOtherMyVC * other = [[QZOtherMyVC alloc]init];
//    other.otherMid = model.mid;
//    [[Tools currentActiveController].navigationController pushViewController:other animated:YES];
    [SVProgressHUDTool showWithStatus:nil];
    KMutableDic(sendDic);
    [sendDic setObject:model.mid forKey:@"videoid"];
    NSMutableArray * videoArr = [NSMutableArray new];
    KWeakSelf
    [YBNetBase userVideoRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        [SVProgressHUDTool dismiss];
        if(code == 200){
            QZVideoModel * model = [[QZVideoModel alloc]init];
            [model setValuesForKeysWithDictionary:data];
            [videoArr addObject:model];
            QZMyDetailsPlayVC * play = [[QZMyDetailsPlayVC alloc]init];
            play.videoArr = videoArr;
            play.videoPlayIndex = 0;
            [[Tools currentActiveController].navigationController pushViewController:play animated:YES];
//            [weakSelf.navigationController pushViewController:play animated:YES];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 2;
}
-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{
    return 1;

}

- (void)followsUser:(NSInteger)row{
    QZUserInfoModel * model = _userList[row];
    KWeakSelf
    KMutableDic(sendDic)
    [sendDic setObject:model.mid forKey:@"to_mid"];
    [YBNetBase userFollowUserRequest:sendDic target:[Tools currentActiveController] successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            if([model.isfollow intValue] == 1){
                model.isfollow = @(0);
                model.total_fans = @([model.total_fans intValue]-1);
                [YBMsgPop showPop:@"已取消关注"];
            }else{
                model.isfollow = @(1);
                model.total_fans = @([model.total_fans intValue]+1);
                [YBMsgPop showPop:NSLocalizedString(@"已关注", nil)];
            }
            [weakSelf.followCollection reloadData];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
@end
